package com.hp.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hp.mapper.ParaMapper;
import com.hp.mapper.SpecMapper;
import com.hp.mapper.TemplateMapper;
import com.hp.pojo.Template;
import com.hp.service.TemplateService;
import com.hp.util.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TemplateServiceImpl implements TemplateService {

    @Resource
    private TemplateMapper templateMapper;

    @Resource
    private SpecMapper specMapper;

    @Resource
    private ParaMapper paraMapper;
    @Override
    public PageResult fingPage(int pageNum, int pageSize, Template template) {
        PageHelper.startPage(pageNum, pageSize);
        Page page = (Page)templateMapper.findAll(template);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    @Override
    public List<Template> findType() {
        return templateMapper.findType();
    }

    @Override
    public void add(Template template) {
        templateMapper.add(template);
    }

    @Override
    public Template findById(int id) {
        return templateMapper.findById(id);
    }

    @Override
    public void update(Template template) {
        templateMapper.update(template);
    }


    @Override
    public void delete(int id) {
        Template template = templateMapper.findById(id);
        if (template.getSpecNum()>0){
            specMapper.deleteSpecIds(id);
        }
        if (template.getParaNum()>0){
            paraMapper.deletePara(id);
        }
        templateMapper.delete(id);
    }
}
